﻿using UnityEngine;

namespace QFramework.Pacman
{
	public partial class GameManager : ViewController
	{
		public enum GameState
		{
			Init,
			Game,
			Dead,
			Scores
		}

		public static GameState gameState;


		[Inject]
		public GameGUINavigation gui { get; set; }
		
		[Inject]
		public PacmanModel Model { get; set; }

		public static bool scared;

		public  float scareLength;
		private float _timeToCalm;

		public float SpeedPerLevel;
		

		//-------------------------------------------------------------------
		// function definitions

		void Awake()
		{
			PacmanGame.Container.RegisterInstance(this);

		}

		void Start()
		{
			gameState = GameState.Init;
			PacmanGame.Container.Inject(this);

		}

		void OnLevelWasLoaded()
		{
			if (Model.Level.Value == 0) Model.Lives.Value = 3;

			Debug.Log("Level " + Model.Level.Value + " Loaded!");
			ResetVariables();


			// Adjust Ghost variables!
			clyde.GetComponent<GhostMove>().speed += Model.Level.Value * SpeedPerLevel;
			blinky.GetComponent<GhostMove>().speed += Model.Level.Value * SpeedPerLevel;
			pinky.GetComponent<GhostMove>().speed += Model.Level.Value * SpeedPerLevel;
			inky.GetComponent<GhostMove>().speed += Model.Level.Value * SpeedPerLevel;
			pacman.GetComponent<PlayerController>().speed += Model.Level.Value * SpeedPerLevel / 2;
		}

		private void ResetVariables()
		{
			_timeToCalm = 0.0f;
			scared = false;
			PlayerController.killstreak = 0;
		}

		// Update is called once per frame
		void Update()
		{
			if (scared && _timeToCalm <= Time.time)
				CalmGhosts();

		}

		public void ResetScene()
		{
			CalmGhosts();

			pacman.transform.position = new Vector3(15f, 11f, 0f);
			blinky.transform.position = new Vector3(15f, 20f, 0f);
			pinky.transform.position = new Vector3(14.5f, 17f, 0f);
			inky.transform.position = new Vector3(16.5f, 17f, 0f);
			clyde.transform.position = new Vector3(12.5f, 17f, 0f);

			pacman.GetComponent<PlayerController>().ResetDestination();
			blinky.GetComponent<GhostMove>().InitializeGhost();
			pinky.GetComponent<GhostMove>().InitializeGhost();
			inky.GetComponent<GhostMove>().InitializeGhost();
			clyde.GetComponent<GhostMove>().InitializeGhost();

			gameState = GameState.Init;
			gui.H_ShowReadyScreen();

		}

		public void ToggleScare()
		{
			if (!scared) ScareGhosts();
			else CalmGhosts();
		}

		public void ScareGhosts()
		{
			scared = true;
			blinky.GetComponent<GhostMove>().Frighten();
			pinky.GetComponent<GhostMove>().Frighten();
			inky.GetComponent<GhostMove>().Frighten();
			clyde.GetComponent<GhostMove>().Frighten();
			_timeToCalm = Time.time + scareLength;

			Debug.Log("Ghosts Scared");
		}

		public void CalmGhosts()
		{
			scared = false;
			blinky.GetComponent<GhostMove>().Calm();
			pinky.GetComponent<GhostMove>().Calm();
			inky.GetComponent<GhostMove>().Calm();
			clyde.GetComponent<GhostMove>().Calm();
			PlayerController.killstreak = 0;
		}

		public void LoseLife()
		{
			Model.Lives.Value--;
			gameState = GameState.Dead;

			// update UI too
			var ui = PacmanGame.Container.Resolve<UIScript>();
			
			Destroy(ui.lives[ui.lives.Count - 1]);
			ui.lives.RemoveAt(ui.lives.Count - 1);
		}

		public static void DestroySelf()
		{
			var model = PacmanGame.Container.Resolve<PacmanModel>();

			model.Score.Value = 0;
			model.Level.Value = 0;
			model.Lives.Value = 3;
			
			Destroy(GameObject.Find("Game Manager"));
		}
	}
}